package cn.neusoft.controller;

import cn.neusoft.domain.Commodity;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.CustomerFeignClient;
import cn.neusoft.query.CommodityQuery;
import cn.neusoft.service.CommodityService;
import cn.neusoft.util.UUIDUtils;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity")
@Api(value = "商品模块的API",description="商品相关的CRUD功能")
public class CommodityController {

    @Autowired
    CustomerFeignClient customerFeignClient;

    @Autowired
    CommodityService commodityService;

    @GetMapping("/all")
    @ApiOperation(value = "查看所有商品信息" )
    public ResultBean getAll(){
        List<Commodity> commodityList = commodityService.getAll();
        return ResultBean.success(commodityList);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加商品" )
    public ResultBean add(@RequestBody Commodity commodity){
        commodity.setCommodity_id(UUIDUtils.getUUID());
        commodityService.add(commodity);
        return ResultBean.success();
    }

    @DeleteMapping("/del/{id}")   // Delete + /stu/1
    @ApiOperation(value = "根据id删除商品" )
    public ResultBean delete(@PathVariable("id") String id){

        if(id==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        int count = customerFeignClient.countMatchingOrders(id);
        if(count == 0){
            commodityService.delete(id);
            return ResultBean.success();
        }
        else{
            return ResultBean.error(AppExceptionCodeMsg.COMMODITY_HAS_ORDER);
        }

    }

    @PutMapping("/mod")   //   Put + /stu
    @ApiOperation(value = "修改商品" )
    public ResultBean update(@RequestBody Commodity commodity){

        if(commodity==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        if(commodity.getCommodity_id()==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        commodityService.update(commodity);
        return ResultBean.success();
    }

    @GetMapping("/get/{id}")    // Get + /stu/1
    @ApiOperation(value = "通过id查询商品信息" )
    public ResultBean<Commodity> getById(@PathVariable("id") String id){

        if(id==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Commodity commodity = commodityService.getById(id);
        if(commodity == null){
            throw new AppException(AppExceptionCodeMsg.NOCommoditys);
        }

        return ResultBean.success(commodity);
    }



    @GetMapping("/warehouseInfo")
    public List<Commodity> getCommoditiesWithWarehouseInfoByName(@RequestParam("name") String name) {
        return commodityService.getCommoditiesWithWarehouseInfoByName(name);
    }

    @PostMapping("/page")  //  Post + /customer/page
    @ApiOperation(value = "分页查询商品信息" )
    public ResultBean findByPage(@RequestBody CommodityQuery commodityQuery){
        PageBean<Commodity> pageBean = commodityService.findByPage(commodityQuery);
        return ResultBean.success(pageBean);
    }

}
